<template>
  <el-container class="home_container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <span>后台管理系统</span>
      </div>
      <el-button type="info" @click="loginOut">退出</el-button>
    </el-header>
    <el-container>
      <!-- 左侧导航 -->
      <el-aside :width="iscollapse ? '64px' : '200px'">
        <div class="hcollapse_menu" @click="collapseBtn()">|||</div>
        <el-menu background-color="#393d49" text-color="#fff" active-text-color="#409eff" unique-opened :collapse='iscollapse' :collapse-transition='false' router :default-active='asideNavUrl'>
          <!-- 一级导航 -->
          <el-submenu :index="firstItem.id + ''" v-for="firstItem in menulist" :key="firstItem.id">
            <template slot="title">
              <i :class="iconobj[firstItem.id]"></i>
              <span>{{firstItem.authName}}</span>
            </template>
            <!-- 二级导航 -->
            <el-menu-item :index=" '/'+secondItem.path" v-for="secondItem in firstItem.children" :key="secondItem.id" @click="setNavStart(secondItem.path)">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{secondItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menulist: [],
      iconobj: {
        125: 'el-icon-s-custom',
        103: 'el-icon-s-tools',
        101: 'el-icon-s-goods',
        102: 'el-icon-s-order',
        145: 'el-icon-s-marketing'
      },
      iscollapse: false,
      asideNavUrl: ''
    }
  },
  created() {
    this.getMenulistData()
    this.asideNavUrl = window.sessionStorage.getItem('activeNavUrl')
  },
  methods: {
    // 退出
    loginOut () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取导航信息
    async getMenulistData() {
      const { data: res } = await this.$http.get('/menus')
      if (res.meta.status !== 200) {
        return this.$message({
          showClose: true,
          message: res.meta.msg,
          type: 'error'
        })
      }
      this.menulist = res.data
    },
    // 点击折叠导航
    collapseBtn() {
      this.iscollapse = !this.iscollapse
    },
    // 点击
    setNavStart(navurl) {
      window.sessionStorage.setItem('activeNavUrl', '/' + navurl)
    }
  }
}
</script>
<style lang="less" scoped>
.home_container{
  height: 100%;
}
.el-header{
  background-color: #23262e;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #ffffff;
  > div {
    display: flex;
    justify-content: space-between;
    > span {
      padding-left: 20px;
    }
  }
}

.el-aside{
  background-color: #393d49;
  .el-menu {
    border-right: none;
  }
}
.el-main{
  background-color: #f2f3f5;
}

.hcollapse_menu{
  text-align: center;
  line-height: 30px;
  color: #ffffff;
  letter-spacing: 0.2em;
  background-color: rgba(255, 255, 255, 0.4);
}
</style>
